﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using MaxLeaf.DB;
using System.Data;
using MySql.Data.MySqlClient;
using System.Data.SqlClient;


namespace MaxLeaf.DAL.W2K
{
    public class region_mdc : BaseDAL
    {
        /// <summary>
        /// 省
        /// </summary>
        private int mintProvLevel = 1;
        private int mintCityLevel = 2;
        private int mintRegionLevel = 3;
        private int mintMDCLevel = 4;
        private int mintCustomerLevel = 5;

        private string GetMaxFnumber(DBAccess dbDestK3Con, string strLevel, string strFNumber)
        {
            //客户编码最后是：   9.省份(2位).城市（2位）.Region(2位).MDC(2位).顺序号(6位)  如: 01.01.01.01.000001
            string strReturn = "";
            string strSQL = "";
            int intPoint = 1;
            if (strLevel == mintCustomerLevel.ToString())  // 客户顺序号
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),9,6) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and FFullName like '" + strFNumber + "%'";
                intPoint = 6;
            }
            else if (strLevel == mintMDCLevel.ToString()) // MDC地址
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),7,2) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and FFullName like '" + strFNumber + "%'";
                intPoint = 2;
            }
            else if (strLevel == mintRegionLevel.ToString()) //区
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),5,2) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and FFullName like '" + strFNumber + "%'";
                intPoint = 2;
            }
            else if (strLevel == mintCityLevel.ToString()) //城市
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),3,2) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and FFullName like '" + strFNumber + "%'";
                intPoint = 2;
            }
            else if (strLevel == mintProvLevel.ToString()) //省份
            {
                strSQL = "select substring(cast(max(cast( replace(fnumber,'.','') as decimal(18,0))) as varchar(50)),1,2) from t_Item where FitemClassID='1' and Flevel='" + strLevel + "' and FFullName like '" + strFNumber + "%'";
                intPoint = 2;
            }
            DataRow drRow = dbDestK3Con.GetDataFirstRow(strSQL);
            if (drRow != null)
            {
                strReturn = BaseFunctions.FormatWithZero(CvtFunc.ToString(CvtFunc.ToDouble(CvtFunc.ToString(drRow[0])) + 1), intPoint);
            }
            return strReturn;
        }

        private void GetWebFItemID(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue,
                                      ref string strWebProvFItemID, ref string strWebCityFItemID, ref string strWebRegionFItemID)
        {
            string strSql = "select province_id,city_mdc_id,region_mdc_id from region_mdc inner join region_main on region_mdc.region_mdc_id = region_main.region_id  where mdc_id='" + strKeyValue + "'";
            DataRow drWebFItemID = dbOrgWebReadCon.GetDataFirstRow(strSql);
            if (drWebFItemID != null)
            {
                strWebProvFItemID = dbOrgWebReadCon.GetValue("select FItemID from k3_region_mdc where region_type='1' and region_mdc_id='" + CvtFunc.ToString(drWebFItemID["province_id"]) + "'");
                strWebCityFItemID = dbOrgWebReadCon.GetValue("select FItemID from k3_region_mdc where region_type='2' and region_mdc_id='" + CvtFunc.ToString(drWebFItemID["city_mdc_id"]) + "'");
                strWebRegionFItemID = dbOrgWebReadCon.GetValue("select FItemID from k3_region_mdc where region_type='3' and region_mdc_id='" + CvtFunc.ToString(drWebFItemID["region_mdc_id"]) + "'");
            }
        }

        public override int Add(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {
            try
            {
                string strmdc_id = strKeyValue;
                //取此mdc的全名
                string strSql = @"SELECT region_mdc.mdc_name,region_main.region_name,region_main_D.region_name as provicename, region_mdc.province_id,region_mdc.mdc_id,
                            k3_db_main.*
                            FROM region_mdc 
                            inner join region_main on city_mdc_id=region_main. region_id
                            inner join region_mdc_db on region_mdc.province_id=region_mdc_db.province_id
                            inner join k3_db_main on region_mdc_db.db_id=k3_db_main.db_id
                            inner join region_main as region_main_D on region_mdc_db.province_id =region_main_D.region_id
                            where region_mdc.mdc_id='{0}'";
                strSql = string.Format(strSql, strKeyValue);
                DataRow drWeb = dbOrgWebReadCon.GetDataFirstRow(strSql);
                if (drWeb != null)
                {                    
                    string strDB = CvtFunc.ToString(drWeb["dbname"]);
                    if (dbDestK3Con.Connection.Database != strDB)
                    {
                        LogManager.Debug(string.Format("不在同一数据库！"));
                        return 1;
                    }

                    string strWebProvFItemID = "", strWebCityFItemID = "", strWebRegionFItemID = "";
                    GetWebFItemID(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strKeyValue, ref strWebProvFItemID, ref strWebCityFItemID, ref strWebRegionFItemID);
                    strSql = "select region_name from region_mdc inner join region_main on region_mdc.region_mdc_id = region_main.region_id  where mdc_id='" + drWeb["mdc_id"] + "'";
                    
                    string strRegionName = dbOrgWebReadCon.GetValue(strSql);
                    string strProvinceName = CvtFunc.ToString(drWeb["provicename"]);
                    string strCityName = CvtFunc.ToString(drWeb["region_Name"]);
                    string strMDCName = CvtFunc.ToString(drWeb["mdc_name"]);
                    
                    string strCityFullName = strProvinceName + "_" + CvtFunc.ToString(drWeb["region_Name"]);  //region_Name 是 "市"
                    string strRegionFullName = strCityFullName + "_" + strRegionName;
                    string strMdcFullName = strRegionFullName + "_" + CvtFunc.ToString(drWeb["mdc_name"]);
                    
                    strSql = "select * from t_Item where FitemclassID=1 and Flevel=" + mintMDCLevel.ToString() + "  and FFullName='{0}'";
                    strSql = string.Format(strSql, strMdcFullName);
                    DataRow drK3MDC = dbDestK3Con.GetDataFirstRow(strSql);
                    if (drK3MDC == null) //如果不存在就新增
                    {
                        strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintProvLevel + "' and FItemID='" + strWebProvFItemID + "'";
                        DataRow drK3Prov = dbDestK3Con.GetDataFirstRow(strSql);
                        if (drK3Prov == null)
                        {
                            throw new MaxLeafException(string.Format("添加MDC不成功！省份不存在。")); 
                        }

                        strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintCityLevel + "' and FItemID='"+ strWebCityFItemID +"'";
                        DataRow drK3City = dbDestK3Con.GetDataFirstRow(strSql);
                        if (drK3City == null) //如果上级城市是不存在，则新增些城市
                        {
                            AddCity(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, drWeb);
                        }
                        else
                        {
                            strSql = "update t_Item set Fname='" + strCityName + "',FFullName='" + strCityFullName + "' where FItemID='" + strWebCityFItemID + "'";
                            dbDestK3Con.ExecuteSql(strSql); 
                        }

                        strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintRegionLevel + "' and FItemID='" + strWebRegionFItemID  + "'";
                        DataRow drK3Region = dbDestK3Con.GetDataFirstRow(strSql);
                        if (drK3Region == null) //如果区不存在，就新增
                        {
                            AddRegion(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, drWeb);
                        }
                        else
                        { 
                            strSql = "update t_Item set Fname='" + strRegionName + "',FFullName='" + strRegionFullName + "' where FItemID='" + strWebRegionFItemID + "'";
                            dbDestK3Con.ExecuteSql(strSql);                                
                        }

                        strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintCityLevel + "' and FFullName='" + strCityFullName + "'";
                        drK3City = dbDestK3Con.GetDataFirstRow(strSql);
                        if (drK3City != null)
                        {
                            string strProvFnumber = CvtFunc.ToString(drK3Prov["Fnumber"]);
                            string strProvFitemID = CvtFunc.ToString(drK3Prov["FitemID"]); 

                            string strCityFnumber = CvtFunc.ToString(drK3City["Fnumber"]);
                            string strCityFItemID = CvtFunc.ToString(drK3City["FitemID"]);
                                                        
                            strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintRegionLevel + "' and FFullName='" + strRegionFullName + "'";
                            drK3Region = dbDestK3Con.GetDataFirstRow(strSql);
                            if (drK3Region != null)
                            {
                                string strRegionFitemID = CvtFunc.ToString(drK3Region["FitemID"]);
                                string strRegionFnumber = CvtFunc.ToString(drK3Region["Fnumber"]);

                                string strMDCNumber = GetMaxFnumber(dbDestK3Con, mintMDCLevel.ToString(), strRegionFullName);
                                string strMDCFNumber = strRegionFnumber + "." + strMDCNumber;
                                                                
                                string strSQL = @"insert into t_Item (FitemClassID,FNumber,FparentID,Flevel,Fdetail,Fname,FUnUsed,FBrNo,FFullNumber,
                                  FDiff,FDeleted,FshortNumber,FFullName) 
                                  VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
                                strSQL = string.Format(strSQL, '1'
                                                                , strMDCFNumber
                                                                , strRegionFitemID
                                                                , mintMDCLevel.ToString()
                                                                , 0
                                                                , CvtFunc.ToString(drWeb["mdc_name"])
                                                                , 0
                                                                , 0
                                                                , strMDCFNumber
                                                                , 0
                                                                , 0
                                                                , strMDCNumber
                                                                , strMdcFullName);
                                strSQL += ";select @@identity";
                                dbDestK3Con.ExecuteSql(strSQL);
                                strSQL = "select FItemID from t_Item where FitemClassID='1' and FFullNumber='" + strMDCFNumber + "' and FFullName='" + strMdcFullName + "'";
                                string strMDCFItemID = dbDestK3Con.GetValue(strSQL);
                                
                                //更新WEB，回写FItemID到网站的mary_mdc表
                                strSQL = "update k3_region_mdc set FItemID='" + strMDCFItemID + "' where region_mdc_id='" + CvtFunc.ToString(drWeb["MDC_ID"]) + "' and region_type='4'";
                                dbOrgWebWriteCon.ExecuteSql(strSQL);
                                return 1;
                            }
                        }
                    }
                    return 1;
                }
            }
            catch (Exception ex)
            {
                throw new MaxLeafException(string.Format("添加MDC不成功！ {0}" ,ex.Message)); 
            }
            return 1;
        }

        private bool AddCity(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, DataRow drWebRow)
        {
            //City的Flevel=2
            string strWebProvFItemID = dbOrgWebReadCon.GetValue("select FItemID from k3_region_mdc where region_type='1' and region_mdc_id='" + CvtFunc.ToString(drWebRow["province_id"]) + "'");
            string strProvinceName = dbOrgWebReadCon.GetValue("select region_name from region_main where region_id='" + CvtFunc.ToString(drWebRow["province_id"]) + "'");
            string strSQL = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintProvLevel + "' and FItemID='" + strWebProvFItemID + "'";
            DataRow drK3Prov = dbDestK3Con.GetDataFirstRow(strSQL);
            if (drK3Prov == null)
            {
                throw new MaxLeafException(string.Format("添加城市不成功！省份不存在。"));
            }

            string strCity = CvtFunc.ToString(drWebRow["region_name"]);
            string strFullName = strProvinceName + "_" + strCity;


            string strProvFnumber = CvtFunc.ToString(drK3Prov["Fnumber"]);
            string strProvFitemID = CvtFunc.ToString(drK3Prov["FitemID"]);

            string strCityFNumber = strProvFnumber + "." + GetMaxFnumber(dbDestK3Con, mintCityLevel.ToString(), strProvinceName);
            string strCityNumber = GetMaxFnumber(dbDestK3Con, mintCityLevel.ToString(), strProvinceName);

            strSQL = @"insert into t_Item (FitemClassID,FNumber,FparentID,Flevel,Fdetail,Fname,FUnUsed,FBrNo,FFullNumber,
                                  FDiff,FDeleted,FshortNumber,FFullName) 
                                  VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
            strSQL = string.Format(strSQL, '1'
                                            , strCityFNumber
                                            , strProvFitemID
                                            , mintCityLevel.ToString()
                                            , 0
                                            , strCity
                                            , 0
                                            , 0
                                            , strCityFNumber
                                            , 0
                                            , 0
                                            , strCityNumber
                                            , strFullName);
            dbDestK3Con.ExecuteSql(strSQL);

            strSQL = "select FItemID from t_Item where FitemClassID='1' and FFullNumber='" + strCityFNumber + "' and FFullName='" + strFullName + "' and Flevel='" + mintCityLevel.ToString() + "'";
            string strCityFItemID = dbDestK3Con.GetValue(strSQL);

            strSQL ="select city_mdc_id from region_mdc where mdc_id='"+ drWebRow["mdc_id"] +"'";
            string strCity_MDC_ID = dbOrgWebReadCon.GetValue(strSQL);

            #region Update City FItemID
            strSQL ="update k3_region_mdc set FItemID='"+ strCityFItemID +"' where Region_Type='2' and region_mdc_id='"+ strCity_MDC_ID +"'";
            dbOrgWebWriteCon.ExecuteSql(strSQL );  
            return true;
            #endregion
        }

        private bool AddRegion(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, DataRow drWebRow)
        {
            //Region的Flevel=3

            string strProvinceName = dbOrgWebReadCon.GetValue("select region_name from region_main where region_id='" + CvtFunc.ToString(drWebRow["province_id"]) + "'");
            string strWebProvFItemID = dbOrgWebReadCon.GetValue("select FItemID from k3_region_mdc where region_type='1' and region_mdc_id='" + CvtFunc.ToString(drWebRow["province_id"]) + "'");
            string strSQL = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintProvLevel + "' and FItemID='" + strWebProvFItemID + "'";
            DataRow drK3Prov = dbDestK3Con.GetDataFirstRow(strSQL);
            if (drK3Prov == null)
            {
                throw new MaxLeafException(string.Format("添加城市不成功！省份不存在。"));
            }

            strSQL = "select region_name from region_mdc inner join region_main on region_mdc.City_mdc_id =region_main.region_id where mdc_id='" + drWebRow["mdc_id"] + "'";//取市的名字
            DataRow drWebCity = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drWebCity == null)
            {
                throw new MaxLeafException(string.Format("添加区域不成功！网站中的市 不存在。"));
            }
            string strCityName = CvtFunc.ToString(drWebRow["region_name"]);

            strSQL = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintCityLevel + "' and FFullName='" + strProvinceName +"_"+ strCityName + "'";
            DataRow drK3City = dbDestK3Con.GetDataFirstRow(strSQL);
            if (drK3City == null)
            {
                throw new MaxLeafException(string.Format("添加城市不成功！市不存在。"));
            }
                      
            string strRegionName = "";
            strSQL = "select region_name from region_mdc inner join region_main on region_mdc.region_mdc_id =region_main.region_id where mdc_id='" + drWebRow["mdc_id"] + "'";//取区的名字
            DataRow drWebRegion = dbOrgWebReadCon.GetDataFirstRow(strSQL);
            if (drWebRegion != null)
            {
                strRegionName = CvtFunc.ToString(drWebRegion["region_name"]);
            }
            else
            {
                throw new MaxLeafException(string.Format("添加区域不成功！网站中的区 不存在。"));
            }
            string strCityFullName = strProvinceName + "_" + strCityName;
            string strFullName = strCityFullName + "_" + strRegionName;
            strSQL = "select region_mdc_id from region_mdc where mdc_id='" + drWebRow["mdc_id"] + "'";
            string strRegion_MDC_ID = dbOrgWebReadCon.GetValue(strSQL);   

            //检查一下原来有没有region存在（按全名检查）
            strSQL = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintRegionLevel + "' and FFullName='" + strFullName + "'";
            DataRow drRegion = dbDestK3Con.GetDataFirstRow(strSQL);
            if (drRegion != null)
            {
                strSQL = "update k3_region_mdc set FItemID='" + drRegion["FitemID"] + "' where Region_Type='3' and region_mdc_id='" + strRegion_MDC_ID + "'";
                dbOrgWebWriteCon.ExecuteSql(strSQL);
                return true;
            }

            string strProvFnumber = CvtFunc.ToString(drK3Prov["Fnumber"]);
            string strProvFitemID = CvtFunc.ToString(drK3Prov["FitemID"]);

            string strCityFNumber = CvtFunc.ToString(drK3City["Fnumber"]);
            string strCityFItemID = CvtFunc.ToString(drK3City["FitemID"]);                        

            string strRegionNumber = GetMaxFnumber(dbDestK3Con, mintRegionLevel.ToString(), strCityFullName);
            string strRegionFNumber = strCityFNumber + "." + strRegionNumber;
            strSQL = @"insert into t_Item (FitemClassID,FNumber,FparentID,Flevel,Fdetail,Fname,FUnUsed,FBrNo,FFullNumber,
                                  FDiff,FDeleted,FshortNumber,FFullName) 
                                  VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')";
            strSQL = string.Format(strSQL, '1'
                                            , strRegionFNumber
                                            , strCityFItemID
                                            , mintRegionLevel.ToString()
                                            , 0
                                            , strRegionName
                                            , 0
                                            , 0
                                            , strRegionFNumber
                                            , 0
                                            , 0
                                            , strRegionNumber
                                            , strFullName);
            dbDestK3Con.ExecuteSql(strSQL);

            strSQL = "select FItemID from t_Item where FitemClassID='1' and FFullNumber='" + strRegionFNumber + "' and FFullName='" + strFullName + "' and Flevel='" + mintRegionLevel.ToString() + "'";
            string strRegionFItemID = dbDestK3Con.GetValue(strSQL);

         

            #region Update Region FItemID
            strSQL = "update k3_region_mdc set FItemID='" + strRegionFItemID + "' where Region_Type='3' and region_mdc_id='" + strRegion_MDC_ID + "'";
            dbOrgWebWriteCon.ExecuteSql(strSQL );  
            return true;
            #endregion
        }


        

        public override int Update(DBAccess dbOrgWebReadCon, DBAccess dbOrgWebWriteCon, DBAccess dbDestK3Con, string strKeyValue)
        {
            try
            {
                string strmdc_id = strKeyValue;
                //取此mdc的全名
                string strSql = @"SELECT region_mdc.mdc_name,region_main.region_name,region_main_D.region_name as provicename, region_mdc.province_id,
                            k3_db_main.*,region_mdc.mdc_id
                            FROM region_mdc 
                            inner join region_main on city_mdc_id=region_main. region_id
                            inner join region_mdc_db on region_mdc.province_id=region_mdc_db.province_id
                            inner join k3_db_main on region_mdc_db.db_id=k3_db_main.db_id
                            inner join region_main as region_main_D on region_mdc_db.province_id =region_main_D.region_id
                            where region_mdc.mdc_id='{0}'";
                strSql = string.Format(strSql, strKeyValue);
                DataRow drWeb = dbOrgWebReadCon.GetDataFirstRow(strSql);
                if (drWeb != null)
                {
                    string strDB = CvtFunc.ToString(drWeb["dbname"]);
                    if (dbDestK3Con.Connection.Database != strDB)
                    {
                        LogManager.Debug(string.Format("不在同一数据库！"));
                        return 1;
                        //throw new MaxLeafException(string.Format("不在同一数据库!"));                        
                    }
                }
                if (drWeb != null)
                {
                    string strWebMDCFItemID = dbOrgWebReadCon.GetValue("select FItemID from k3_region_mdc where region_type='4' and region_mdc_id='" + strKeyValue + "'");
                    if (CvtFunc.ToInt(strWebMDCFItemID) == 0)
                    {
                        return Add(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strKeyValue);
                    }
                    strSql = "select region_name from region_mdc inner join region_main on region_mdc.region_mdc_id = region_main.region_id  where mdc_id='" + drWeb["mdc_id"] + "'";
                    string strRegionName = dbOrgWebReadCon.GetValue(strSql);

                    string strProvinceName = CvtFunc.ToString(drWeb["provicename"]);
                    string strWebProvFItemID = "", strWebCityFItemID = "", strWebRegionFItemID = "";
                    GetWebFItemID(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, strKeyValue, ref strWebProvFItemID, ref strWebCityFItemID, ref strWebRegionFItemID);

                    string strCityName = CvtFunc.ToString(drWeb["region_Name"]);
                    string strMDCName = CvtFunc.ToString(drWeb["mdc_name"]);
                    
                    string strCityFullName = strProvinceName + "_" + CvtFunc.ToString(drWeb["region_Name"]);  //region_Name 是 "市"
                    string strRegionFullName = strCityFullName + "_" + strRegionName;
                    string strMdcFullName = strRegionFullName + "_" + CvtFunc.ToString(drWeb["mdc_name"]);

                    strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintProvLevel + "' and FItemID='" + strWebProvFItemID + "'";
                    DataRow drK3Prov = dbDestK3Con.GetDataFirstRow(strSql);
                    if (drK3Prov == null)
                    {
                        throw new MaxLeafException(string.Format("添加MDC不成功！省份不存在。"));
                    }

                    strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintCityLevel + "' and FItemID='" + strWebCityFItemID + "'";
                    DataRow drK3City = dbDestK3Con.GetDataFirstRow(strSql);
                    if (drK3City == null) //如果上级城市是不存在，则新增些城市
                    {
                        AddCity(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, drWeb);
                    }
                    else
                    {
                        strSql = "update t_Item set Fname='" + strCityName + "',FFullName='" + strCityFullName + "' where FItemID='" + strWebCityFItemID + "'";
                        dbDestK3Con.ExecuteSql(strSql);
                    }

                    strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintRegionLevel + "' and FItemID='" + strWebRegionFItemID + "'";
                    DataRow drK3Region = dbDestK3Con.GetDataFirstRow(strSql);
                    if (drK3Region == null) //如果区不存在，就新增
                    {
                        AddRegion(dbOrgWebReadCon, dbOrgWebWriteCon, dbDestK3Con, drWeb);
                    }
                    else
                    {
                        strSql = "update t_Item set Fname='" + strRegionName + "',FFullName='" + strRegionFullName + "' where FItemID='" + strWebRegionFItemID + "'";
                        dbDestK3Con.ExecuteSql(strSql);
                    }

                    strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintCityLevel + "' and FFullName='" + strCityFullName + "'";
                    drK3City = dbDestK3Con.GetDataFirstRow(strSql);
                    if (drK3City != null)
                    {
                        string strProvFnumber = CvtFunc.ToString(drK3Prov["Fnumber"]);
                        string strProvFitemID = CvtFunc.ToString(drK3Prov["FitemID"]);

                        string strCityFnumber = CvtFunc.ToString(drK3City["Fnumber"]);
                        string strCityFItemID = CvtFunc.ToString(drK3City["FitemID"]);

                        strSql = "select Fnumber,FitemID from t_Item where FitemclassID=1 and Flevel='" + mintRegionLevel + "' and FFullName='" + strRegionFullName + "'";
                        drK3Region = dbDestK3Con.GetDataFirstRow(strSql);
                        if (drK3Region != null)
                        {
                            string strRegionFitemID = CvtFunc.ToString(drK3Region["FitemID"]);
                            string strRegionFnumber = CvtFunc.ToString(drK3Region["Fnumber"]);
                            
                            string strMDCNumber = "";
                            string strMDCFNumber = "";

                            //strSql = "select FFullName,Fnumber from t_Item where FitemclassID=1 and Flevel='" + mintMDCLevel + "' and FFullName='" + strMdcFullName + "'";
                            strSql = "select FFullName,Fnumber from t_Item where FitemclassID=1 and Flevel='" + mintMDCLevel + "' and FItemID='" + strWebMDCFItemID + "'";
                            DataRow drMDC = dbDestK3Con.GetDataFirstRow( strSql);

                            if (drMDC != null && (CvtFunc.ToLeftString(CvtFunc.ToString(drMDC["FFullName"]), strRegionFullName.Length) + "_").Equals(strRegionFullName + "_", StringComparison.InvariantCultureIgnoreCase)) //没有更新上级组
                            {
                                 
                                //strMdcFullName =  CvtFunc.ToString(drMDC["FFullName"]);
                                strMDCFNumber = CvtFunc.ToString(drMDC["Fnumber"]);
                            }
                            else //更换了上级组，需要重新取FullNumber和FullName
                            {
                                strMDCNumber = GetMaxFnumber(dbDestK3Con, mintMDCLevel.ToString(), strRegionFullName);
                                strMDCFNumber = strRegionFnumber + "." + GetMaxFnumber(dbDestK3Con, mintMDCLevel.ToString(), strRegionFullName);
                            }

                            string strOldMDCFNumber = dbDestK3Con.GetValue("select FNumber from t_Item where FItemID='" + strWebMDCFItemID + "'");

                            string strSQL = @"UPDATE t_Item set FitemClassID='{0}'
                                        ,FNumber='{1}'
                                        ,FparentID='{2}'
                                        ,Flevel='{3}'
                                        ,Fdetail='{4}'
                                        ,Fname='{5}'
                                        ,FUnUsed='{6}'
                                        ,FBrNo='{7}'
                                        ,FFullNumber='{8}'
                                        ,FDiff='{9}'
                                        ,FDeleted='{10}'
                                        ,FshortNumber='{11}'
                                        ,FFullName='{12}'
                                        where FItemID='" + strWebMDCFItemID + "'";
                            strSQL = string.Format(strSQL, '1'
                                                          , strMDCFNumber
                                                          , strRegionFitemID
                                                          , mintMDCLevel.ToString ()
                                                          , 0
                                                          , strMDCName 
                                                          , 0
                                                          , 0
                                                          , strMDCFNumber
                                                          , 0
                                                          , 0
                                                          , strMDCNumber
                                                          , strMdcFullName);
                            dbDestK3Con.ExecuteSql(strSQL);

                            strSql = @"update t_item set ffullname='' where fnumber like @fnumber and fitemclassid=1";
                            dbDestK3Con.ExecuteSql(strSql
                                , new SqlParameter[] { new SqlParameter("@fnumber", strMDCFNumber + ".%") });

                            if (!strOldMDCFNumber.Equals(strMDCFNumber, StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (!strOldMDCFNumber.Split('.').Length.Equals(strMDCFNumber.Split('.').Length))
                                {
                                    throw new MaxLeafException(string.Format("Cannot change fnumber {0} to {1}", strOldMDCFNumber, strMDCFNumber));
                                }

                                strSql = "Update t_Item Set FNumber = {fn INSERT(FNumber, 1, @Len, @FNumber)},FFullNumber = {fn INSERT(FNumber, 1, @Len, @FNumber)} Where FNumber Like @OldFNumber AND FItemClassID = @FItemClassID";
                                KeyValuePair<string, IDataParameter[]> kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "Len", strMDCFNumber.Length }, { "Fnumber", strMDCFNumber }, { "OldFNumber", strOldMDCFNumber + ".%" }, { "FItemClassID", "1" } });
                                dbDestK3Con.ExecuteSql(strSql, kvp.Value);
                                 
                                    strSql = string.Format("UPDATE {0} Set FNumber =i.F1  From ( Select FNumber F1,FItemID FID1,FParentID FPID1 From t_Item Where FNumber Like @FNumber AND FItemClassID =  @FItemClassID ) i Where FItemID=i.FID1 "
                                        , "t_organization"
                                        );
                                    kvp = dbDestK3Con.CreateCondParam(new Dictionary<string, object>() { { "FNumber", strMDCFNumber + ".%" }, { "FItemClassID", "1" } });
                                    dbDestK3Con.ExecuteSql(strSql, kvp.Value);
                                 
                            }
                        }
                    }
                }
                return 1;
            }
            catch (Exception ex)
            {
                throw new MaxLeafException(string.Format("添加MDC不成功！{0}",ex.Message));                
            }
            finally
            {

            }
        }
    }
}
